<?php
class StreamAddAction extends Action{
	
	public function main(){
		$this->_request['action'] = isset($this->_request['action']) ? $this->_request['action'] : 'default';
		switch ($this->_request['action']) {
			case 'modify':
				$this->__modify();
				break;
			default:
				$this->__load();
				break;
		}

	}

	private function __load(){
		$row = array();
		if (isset($this->_request['StreamID'])) {
			$StreamID = $this->_request['StreamID'];
			$obj = new  StreamInfoDAO();
			$row =$obj->getOneByID($StreamID);	
			
			foreach ($row as $key => $value) {
				$this->_response[$key] = $value;
			}
		}
		$this->_response['ClassID'] = $this->_request['ClassID'];
		$this->_response['ChannelID'] = $this->_request['defChannelID'];
		$selected = new Selected();
		$this->_response['PlayMode'] = $selected->get("PlayMode",$this->_response['PlayMode']);
		$this->_response['StreamEnable'] = $selected->get("StreamEnable",$this->_response['StreamEnable']);
		$this->_response['StreamDisplay'] = $selected->get("StreamDisplay",$this->_response['StreamDisplay']);
		$this->_response['StreamDisplayOrder'] = $selected->get("StreamDisplayOrder",$this->_response['StreamDisplayOrder']);
	}


	private function __modify(){
		$inputarr =  array();
		//debug($this->_request);
		$notindb = array("do",'action','StreamID','ClassID','ChannelID');
		foreach ($this->_request as $k => $v) {
			if (!in_array($k, $notindb)){
				$inputarr[$k] = $v;
			}
		}
		//debug($inputarr);
		$obj2 = new ChannelStreamRelationDAO();
		$StreamID = (int)$this->_request['StreamID']?(int)$this->_request['StreamID'] : 0;
		$obj = new  StreamInfoDAO();
		if ($StreamID){
			$ret = $obj->modify($StreamID,$inputarr);
		}else {
			$ret = $obj->add($inputarr);
			$insertid = $obj->lastID();
			$newarr ['StreamID'] = $insertid;
			$newarr ['ChannelID'] = $this->_request['ChannelID'];
			$obj2->add($newarr);
		}

		$url = "index.php?do=Channel/channel_list&ClassID=".$this->_request['ClassID']."&list=stream";
		//debug($url);
		//die();
		header("Location:$url");

		die();

	}




}